package com.education.webservlet;

/**
 * Created by Fan on 2016/9/27.
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ThreadPoolExecutor;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.education.db.dao.*;
import com.education.db.domain.*;
import com.education.service.bean.ActivityBean;
import com.education.util.MD5Util;
import org.apache.logging.log4j.Logger;
import org.hibernate.Transaction;

import com.education.db.HibernateSessionFactory;
//import com.education.db.dao.InfoUscodeDAO;
//import com.education.db.dao.LogUsloginDAO;
//import com.education.db.domain.InfoUscode;
//import com.education.db.domain.LogUslogin;
//import com.education.pingppservlet.PayProcessor;
import com.education.service.bean.UserBean;
//import com.education.servlet.AsyncListener;
//import com.education.servlet.AsyncRequestProcessor;
import com.education.util.Constant;

public class UserLoginServlet extends HttpServlet {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private Transaction tran;

    public UserLoginServlet() {

        super();
    }

    public void destroy() {
        super.destroy();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding(Constant.CHAR_ENCODING);

        tran = HibernateSessionFactory.getSession().getTransaction();

        String phone = request.getParameter("phone");
        String password = request.getParameter("password");
        password = MD5Util.MD5(password);

        try {
            tran.begin();
            InfoUserDAO infoUserDAO = new InfoUserDAO();
            InfoUser infoUser = (InfoUser) infoUserDAO.findByUsPhone(phone);
            if (infoUser == null) {
                String info = "用户不存在";
                request.setAttribute("info", info);
                request.getRequestDispatcher("/Pages/message.jsp").forward(request,
                        response);
            } else if (!infoUser.getUsPassword().equals(password)) {
                String info="密码错误";
                request.setAttribute("info", info);
                request.getRequestDispatcher("/Pages/message.jsp").forward(request,
                        response);

            } else {
//                userLoginLog(infoUser);
                UserBean userBean = new UserBean();
                userBean.setPhone(infoUser.getUsPhone());
                userBean.setUserID(infoUser.getUsId());
                request.getSession().setAttribute("UserBean", userBean);
//                request.getRequestDispatcher("/user/Main.jsp").forward(request, response);

//                获取数据库中的四个分类，参照InitServlet.java
                InfoCatagoryDAO infoCatagoryDAO = new InfoCatagoryDAO();
                InfoSchoolDAO infoSchoolDAO = new InfoSchoolDAO();
                InfoMajorDAO infoMajorDAO = new InfoMajorDAO();
                InfoCourseDAO infoCourseDAO = new InfoCourseDAO();
                List<InfoCatagory> infoCatagories = infoCatagoryDAO.findAll();
                List<ActivityBean> infoCatagoryBean = new ArrayList<>();
                List<InfoSchool> infoSchools = infoSchoolDAO.findAll();
                List<ActivityBean> infoSchoolBean = new ArrayList<>();
                List<InfoMajor> infoMajors = infoMajorDAO.findAll();
                List<ActivityBean> infoMajorBean = new ArrayList<>();
                List<InfoCourse> infoCourses = infoCourseDAO.findAll();
                List<ActivityBean> infoCourseBean =new ArrayList<>();
                for(InfoCatagory infoCatagory:infoCatagories)
                {
                    ActivityBean infocatagorybean = new ActivityBean();
                    infocatagorybean.setId(infoCatagory.getCaId());
                    infocatagorybean.setValue(infoCatagory.getCaValue());
                    infocatagorybean.setName(infoCatagory.getCaName());
                    infoCatagoryBean.add(infocatagorybean);
                }
                for(InfoSchool infoSchool:infoSchools)
                {
                    ActivityBean infoschoolbean = new ActivityBean();
                    infoschoolbean.setId(infoSchool.getScId());
                    infoschoolbean.setValue(infoSchool.getScValue());
                    infoschoolbean.setName(infoSchool.getScName());
                    infoSchoolBean.add(infoschoolbean);
                }
                for(InfoMajor infoMajor:infoMajors)
                {
                    ActivityBean infomajorbean = new ActivityBean();
                    infomajorbean.setId(infoMajor.getMaId());
                    infomajorbean.setValue(infoMajor.getMaValue());
                    infomajorbean.setName(infoMajor.getMaName());
                    infoMajorBean.add(infomajorbean);
                }
                for(InfoCourse infoCourse:infoCourses)
                {
                    ActivityBean infomajorbean = new ActivityBean();
                    infomajorbean.setId(infoCourse.getCoId());
                    infomajorbean.setValue(infoCourse.getCoValue());
                    infomajorbean.setName(infoCourse.getCoName());
                    infoCourseBean.add(infomajorbean);
                }
                request.setAttribute("infocatagorybean",infoCatagoryBean);
                request.setAttribute("infoschoolbean",infoSchoolBean);
                request.setAttribute("infomajorbean",infoMajorBean);
                request.setAttribute("infocoursebean",infoCourseBean);
                request.getRequestDispatcher("/user/Main.jsp").forward(request,response);
                // response.sendRedirect("/user/Main.jsp");

                tran.commit();
            }
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println(e.toString());
            tran.rollback();
        }
    }

//    private InfoUscode getAuthcode(InfoUser infoUser,
//                                   InfoUscodeDAO infoUsercodeDao) {
//        InfoUscode infoUscode = new InfoUscode();
//        infoUscode.setUsId(infoUser.getUsId());
//        infoUscode.setInfoUser(infoUser);
//        String authcode = UUID.randomUUID().toString().replace("-", "");
//        infoUscode.setUscoCode(authcode);
//        infoUsercodeDao.save(infoUscode);
//        return infoUscode;
//    }

//    private void userLoginLog(InfoUser infoUser) {
//        LogUsloginDAO logUsloginDAO = new LogUsloginDAO();
//        LogUslogin logUslogin = new LogUslogin();
//        logUslogin.setInfoUser(infoUser);
//        logUslogin.setLogusLogdate(System.currentTimeMillis());
//        logUsloginDAO.save(logUslogin);
//    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException
     *             if an error occurs
     */
    public void init() throws ServletException {

    }

}
